package com.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dao.PermissionDao;
import com.dao.RoleDao;
import com.dao.UserDao;
import com.pojo.Permission;
import com.pojo.Role;
import com.pojo.User;
import com.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Set;


@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    //根据用户名查询用户信息，以及用户角色 和用户角色关联的用户权限
    public User findByUsername(String username) {
        //根据用户名查询用户信息
        User user = userDao.findByUsername(username);
        if (user == null){
            return null;
        }
        //根据用户id查询用户的角色
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            //根据角色id查询权限
            Integer roleId = role.getId();
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }
}
